#!/usr/bin/env bash
name='front-end'
port=54321
version=$(date "+%Y-%m-%d-%H-%M-%S")
docker build -t "$name:$version" .
echo "构建镜像成功"

CONTAINER_ID=$(docker ps -a | grep "$name" | awk '{print $1}')

if [[ ${CONTAINER_ID} ]]; then
  echo "存在$name容器，CONTAINER_ID=$CONTAINER_ID"
  if docker stop "$CONTAINER_ID"; then
    echo "停止$name容器，CONTAINER_ID=$CONTAINER_ID"
    docker rm "$CONTAINER_ID"
    echo "移除$name容器，CONTAINER_ID=$CONTAINER_ID"
  fi
else
  echo "不存在$name容器"
fi

docker run --name "$name" -p "$port":80 -d "$name:$version"
echo "启动容器成功"
